import { getRoleList, getCategoryGetAuthorities } from '@/api';
import { useAppStore } from '@/store/app';
import { ADMIN_ROLECODE, APPROVE0_ROLECODE, APPROVE1_ROLECODE, APPROVE2_ROLECODE } from '@/constants/config';

/** 角色列表 */
const roleCodeList = ref<any>([]);

/** 查询场景分类权限-配置岗/复核/终审 */
const permissionerRole = ref<Object>({});

export const useRole = (roleCode: string | number, categoryId: string) => {

  const appStore = useAppStore();

  const getUserList = async(roleCode: string) => {
    const res = await getUserQuery({ roleCode });
    if (res) {
      const list = res || [];
      roleCodeList.value = list;
    }
  };

  const flowDisabled = async() => {
    const res = await getCategoryGetAuthorities({ categoryId });
    if (res) {
      const permissionRoles = {
        adminRole: res.find(item => Number(item.roleId) === ADMIN_ROLECODE && item.accountNames.includes(appStore.mcisaas?.accountName)) ? true : false,
        configRole: res.find(item => Number(item.roleId) === APPROVE0_ROLECODE && item.accountNames.includes(appStore.mcisaas?.accountName)) ? true : false,
        checkRole: res.find(item => Number(item.roleId) === APPROVE1_ROLECODE && item.accountNames.includes(appStore.mcisaas?.accountName)) ? true : false,
        approveRole: res.find(item => Number(item.roleId) === APPROVE2_ROLECODE && item.accountNames.includes(appStore.mcisaas?.accountName)) ? true : false
      };
      permissionerRole.value = permissionRoles;
      return Promise.resolve(permissionRoles);
    } else {
      return Promise.resolve(null);
    }
  };

  return {
    roleCodeList,
    permissionerRole,
    initFlowDisabled: () => flowDisabled()
  };
};
